/**
 * Alibaba.com.cn Inc.
 * Copyright (c) 2004-2021 All Rights Reserved.
 */
package com.hiv.controller.admin;

import cn.hutool.core.convert.Convert;
import com.hiv.common.BaseController;
import com.hiv.common.PageInfo;
import com.hiv.common.WebResponse;
import com.hiv.common.exception.BusinessException;
import com.hiv.common.param.RoleAddParam;
import com.hiv.common.param.RoleUpdateParam;
import com.hiv.common.param.page.PageQueryRoleParam;
import com.hiv.common.response.RolePermissionVO;
import com.hiv.mapper.domain.Admin;
import com.hiv.mapper.domain.Permission;
import com.hiv.mapper.domain.Role;
import com.hiv.service.AdminAccountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author zhanghong
 * @version AdminRoleController.java, v 0.1 2022/9/18 21:27 下午 AdminRoleController Exp $$
 */
@Slf4j
@RestController
@RequestMapping("/admin/role")
@Api(value = "角色管理模块", tags = {"【管理端】角色管理模块"})
public class AdminRoleController extends BaseController {

    @Autowired
    private AdminAccountService adminAccountService;

    @ApiOperation(value = "新增", httpMethod = "POST")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "success")})
    @PostMapping(path = "/add")
    public WebResponse<Boolean> add(HttpServletRequest request, @RequestBody RoleAddParam param) throws BusinessException {
        Admin admin = validAdminLoginAndGet(request);
        param.setOperator(admin.getName());
        adminAccountService.addRole(admin, param);
        return new WebResponse<>(true);
    }

    @ApiOperation(value = "编辑", httpMethod = "POST")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "success")})
    @PostMapping(path = "/edit")
    public WebResponse<Boolean> edit(HttpServletRequest request, @RequestBody RoleUpdateParam param) throws BusinessException {
        Admin admin = validAdminLoginAndGet(request);
        param.setOperator(admin.getName());
        adminAccountService.updateRole(param);
        return new WebResponse<>(true);
    }

    @ApiOperation(value = "删除角色", httpMethod = "POST")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "success")})
    @PostMapping(path = "/delete")
    public WebResponse<Boolean> removeRole(HttpServletRequest request, @RequestParam String ids) throws BusinessException {
        validAdminLoginAndGet(request);
        String[] strings = Convert.toStrArray(ids);
        adminAccountService.removeRole(Long.parseLong(strings[0]));
        return new WebResponse<>(true);
    }

    @ApiOperation(value = "查询所有角色", httpMethod = "POST")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "success")})
    @PostMapping(path = "/list/all")
    public WebResponse<List<Role>> listAllRole(HttpServletRequest request, @RequestBody Role param) throws BusinessException {
        Admin admin = validAdminLoginAndGet(request);
        param.setSuperAdminId(admin.getId());
        List<Role> roles = adminAccountService.listAllRole(param);
        return new WebResponse<>(roles);
    }

    @ApiOperation(value = "分页查询角色", httpMethod = "POST")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "success")})
    @PostMapping(path = "/pageQuery")
    public WebResponse<PageInfo<RolePermissionVO>> pageQueryRole(HttpServletRequest request, @RequestBody PageQueryRoleParam param) throws BusinessException {
        Admin admin = validAdminLoginAndGet(request);
        PageInfo<RolePermissionVO> pageInfo = adminAccountService.pageQueryRole(admin ,param);
        return new WebResponse<>(pageInfo);
    }

    @ApiOperation(value = "查询所有权限", httpMethod = "POST")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "success")})
    @PostMapping(path = "/list/all/permission")
    public WebResponse<List<Permission>> listAllPermission(HttpServletRequest request) throws BusinessException {
        validAdminLoginAndGet(request);
        List<Permission> permissions = adminAccountService.listAllPermission();
        return new WebResponse<>(permissions);
    }

    @ApiOperation(value = "查询医院所有权限", httpMethod = "POST")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "success")})
    @PostMapping(path = "/list/sub/permission")
    public WebResponse<List<Permission>> listSubPermission(HttpServletRequest request) throws BusinessException {
        validAdminLoginAndGet(request);
        List<Permission> permissions = adminAccountService.listSubPermission();
        return new WebResponse<>(permissions);
    }
}
